package com.chris.blog.controller;

import com.chris.blog.dao.InviteDao;
import com.chris.blog.entity.po.InvitePO;
import com.chris.blog.entity.po.UserInfoPO;
import com.chris.blog.entity.po.UserPO;
import com.chris.blog.service.UserInfoService;
import com.chris.blog.service.UserService;
import com.chris.blog.entity.vo.UserForm;
import com.chris.blog.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private InviteDao inviteDAO;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @GetMapping("index")
    public String index() {
        return "index";
    }

    @GetMapping("toLogin")
        public String toLogin() {
        return "login";
    }

    @GetMapping("register")
    public String register() {
        return "register";
    }

    @PostMapping("/register")
    public String register(UserForm userForm, Model model) {
        // 判断用户是否已经存在
        if (userService.findUserByNme(userForm.getUsername()) != null) {
            model.addAttribute("regMsg", "用户名已存在");
            return "register";
        }
        // 判断密码是否都相同
        if (!userForm.getPassword().equals(userForm.getRepassword())) {
            model.addAttribute("regMsg", "两次密码不相同,请重新输入");
            return "register";
        }
        // 根据邀请码查库 看是否被使用过
        InvitePO invitePO = inviteDAO.findCode(userForm.getCode());
        if (invitePO == null) {
            model.addAttribute("regMsg", "该邀请码不存在");
            return "register";
        } else {
            // 1表示被使用过
            if (invitePO.getStatus() == 1) {
                model.addAttribute("regMsg", "该邀请码不存在");
                return "register";
            } else {
                // 接下来就可以注册用户了
                UserPO userPO = new UserPO();
                Long uid = SnowflakeIdWorker.nextId();
                userPO.setUid(uid);
                // 默认就是普通用户
                userPO.setRoleId(2);
                userPO.setUsername(userForm.getUsername());
                userPO.setPassword(passwordEncoder.encode(userForm.getPassword()));
                Date nowDate = new Date();
                userPO.setGmtCreate(nowDate);
                userPO.setLoginDate(nowDate);
                // 保存对象
                userService.insertSelective(userPO);
                System.out.println("注册成功");

                // userinfo需要设置uid
                UserInfoPO userInfoPO = new UserInfoPO();
                userInfoPO.setUid(userPO.getUid());
                userInfoService.insert(userInfoPO);

                // 激活邀请码
                invitePO.setActiveTime(nowDate);
                invitePO.setStatus(1);
                invitePO.setUid(userPO.getUid());
                inviteDAO.updateByPrimaryKeySelective(invitePO);
                return "redirect:/toLogin";
            }


        }

    }
}
